package edu.tsinghua.mrpvc.test;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

import edu.tsinghua.mrpvc.device.LightDevice;
import edu.tsinghua.mrpvc.device.light.LightDeviceImpl;
import edu.tsinghua.mrpvc.test.AC.ACTest;
import edu.tsinghua.mrpvc.test.light.LightTest;

public class Activator implements BundleActivator {

	private final static String[] LIGHT_RESPONSE_TOPICS = {LightDevice.EVENT_TOPIC_ISVALID, LightDevice.EVENT_TOPIC_LEVEL, LightDevice.EVENT_TOPIC_POWER};
	private final static String[] TEST_RESPONSE_TOPICS = {LightDevice.EVENT_TOPIC_STATE};
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		LightDevice[] lights = new LightDeviceImpl[] {new LightDeviceImpl(context, "1")};
		
		if (lights != null) {			
			Dictionary<String, Object> property = new Hashtable<String, Object>();
			property.put(EventConstants.EVENT_TOPIC, LIGHT_RESPONSE_TOPICS);
			
			for (int i=0; i<lights.length; ++i) {
				context.registerService(LightDevice.class.getName(), lights[i], null);
				context.registerService(EventHandler.class.getName(), lights[i], property);
			}
		}
		
		LightTest test = new LightTest(context);
		Dictionary<String, Object> property = new Hashtable<String, Object>();
		property.put(EventConstants.EVENT_TOPIC, TEST_RESPONSE_TOPICS);
		context.registerService(EventHandler.class.getName(), test, property);
		
		ACTest acTest = new ACTest(context);
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
	}

}
